package com.itheima.web.controller.pms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.exception.CustomException;
import com.itheima.service.pms.ProductAttributeCategoryService;
import com.itheima.utils.PageResult;
import com.itheima.utils.ResultData;
import com.itheima.vo.pms.PmsProductAttributeCategory;
import com.itheima.vo.pms.PmsProductAttributeCategoryDto;
import com.itheima.vo.pms.Product;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/productAttribute/category")
public class ProductAttributeCategoryController {

    @DubboReference
    public ProductAttributeCategoryService categoryService;

    /**
     * 添加商品属性分类
     */
    @PostMapping("create")
    public ResultData create(PmsProductAttributeCategory ppac){
        //逻辑判断  1.去重  2.在直接添加数据
        PmsProductAttributeCategory one = categoryService.getOneByName(ppac.getName());
        if (one!=null){
            //返回添加失败
            return ResultData.error("标签名重复");
        }

        categoryService.save(ppac);
        return ResultData.ok("添加成功",null);
    }

    /**
     * 删除单个商品属性分类
     */
    @GetMapping("delete/{id}")
    public ResultData delete(@PathVariable int id){
        //判断是否有关联的商品
        List<Product> list=categoryService.getByIdWithProduct(id);
        if (list.size()!=0){
            return ResultData.error("有关联的商品,不能删除");
        }

        //直接调用方法删除
        boolean b = categoryService.removeById(id);
        return true? ResultData.ok("删除成功",null):ResultData.error("删除失败");
    }

    /**
     * 分页获取所有商品属性分类
     */
    @GetMapping("list")
    public ResultData list(int pageNum, int pageSize){

        pageNum=(pageNum-1)*pageSize;

        long count = categoryService.count();

        List<PmsProductAttributeCategory> list=categoryService.pageInfo2(pageNum,pageSize);

        PageResult init = PageResult.init(count, (long) pageSize, (long) pageNum, list);

        return ResultData.ok("操作成功",init);
    }

    /**
     * 修改商品属性分类
     */
    @PostMapping("update/{id}")
    public ResultData update(@PathVariable int id,@RequestParam String name){
        //修改名称

        boolean b = categoryService.updateBy(id,name);

        if (!b){
            return ResultData.error("修改失败");
        }

        return ResultData.ok("修改成功",null);
    }

    /**
     * 获取单个商品属性分类信息
     */
    @GetMapping("{id}")
    public ResultData get(@PathVariable int id){
        PmsProductAttributeCategory byId = categoryService.getById(id);
        return ResultData.ok("",byId);
    }

    /**
     * 获取所有商品属性分类及其下属性
     */
    @GetMapping("list/withAttr")
    public ResultData withAttr(){
        List<PmsProductAttributeCategory> list=categoryService.get();

        List<PmsProductAttributeCategoryDto> categoryList=list.stream().map((item)->{
            PmsProductAttributeCategoryDto pms=new PmsProductAttributeCategoryDto();
            BeanUtils.copyProperties(item,pms);

            pms.setProductAttributeList(categoryService.getByCAId(item.getId()));

            return pms;
        }).collect(Collectors.toList());

        return ResultData.ok("查询成功",categoryList);
    }
}
